/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.model;

import com.google.appengine.api.datastore.Key;
import za.org.foobar.famon.util.DB;

import javax.jdo.annotations.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Carl Crous
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Property {
    @NotPersistent
    private static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssZ");

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private String name;

    @Persistent
    private String value;

    public Property() {
    }

    public Property(String name) {
        this.name = name;
    }

    public Property(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public static Property getProperty(String name) {
        List<Property> list = DB.createDBQuery(Property.class)
                .addWhere("name == nameParam")
                .addParameter("nameParam", name)
                .getResultList();
        if (list.size() == 0) {
            return null;
        } else {
            return list.get(0);
        }
    }

    public static Property setProperty(String name, String value) {
        Property p = getProperty(name);
        if (p == null) {
            p = new Property(name, value);
        }
        DB.persist(p);
        return p;
    }

    public Key getId() {
        return id;
    }

    public void setId(Key id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public static boolean getBooleanProperty(String name, boolean defaultValue) {
        DB.begin();
        try {
            Property p = getProperty(name);
            if (p == null) {
                p = new Property(name);
                p.setValue(defaultValue);
                DB.persist(p);
                return defaultValue;
            }
            return p.getBooleanValue(defaultValue);
        } finally {
            DB.commit();
        }
    }
    
    public boolean getBooleanValue(boolean defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        return Boolean.parseBoolean(value);
    }

    public void setValue(boolean b) {
        value = Boolean.toString(b);
    }

    public static int getIntegerProperty(String name, int defaultValue) {
        Property p = getProperty(name);
        if (p == null) {
            p = new Property(name);
            p.setValue(defaultValue);
            DB.persist(p);
            return defaultValue;
        }
        return p.getIntegerValue(defaultValue);
    }
    
    public int getIntegerValue(int defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        return Integer.parseInt(value);
    }

    public void setValue(int i) {
        value = Integer.toString(i);
    }

    public static float getFloatProperty(String name, float defaultValue) {
        Property p = getProperty(name);
        if (p == null) {
            p = new Property(name);
            p.setValue(defaultValue);
            DB.persist(p);
            return defaultValue;
        }
        return p.getFloatValue(defaultValue);
    }
    
    public float getFloatValue(float defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        return Float.parseFloat(value);
    }

    public void setValue(float f) {
        value = Float.toString(f);
    }

    public static double getDoubleProperty(String name, double defaultValue) {
        Property p = getProperty(name);
        if (p == null) {
            p = new Property(name);
            p.setValue(defaultValue);
            DB.persist(p);
            return defaultValue;
        }
        return p.getDoubleValue(defaultValue);
    }
    
    public double getDoubleValue(double defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        return Double.parseDouble(value);
    }

    public void setValue(double d) {
        value = Double.toString(d);
    }

    public static Date getDateProperty(String name, Date defaultValue) {
        Property p = getProperty(name);
        if (p == null) {
            p = new Property(name);
            p.setValue(defaultValue);
            DB.persist(p);
            return defaultValue;
        }
        return p.getDateValue(defaultValue);
    }
    
    public Date getDateValue(Date defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        try {
            return dateFormat.parse(value);
        } catch (ParseException e) {
            return null;
        }
    }

    public void setValue(Date d) {
        value = dateFormat.format(d);
    }
}
